home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / molehill.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  4KB  |  145 lines

  1. #include <GL/glut.h>
  2.  
  3. GLfloat mat_red_diffuse[] = { 0.7, 0.0, 0.1, 1.0 };
  4. GLfloat mat_green_diffuse[] = { 0.0, 0.7, 0.1, 1.0 };
  5. GLfloat mat_blue_diffuse[] = { 0.0, 0.1, 0.7, 1.0 };
  6. GLfloat mat_yellow_diffuse[] = { 0.7, 0.8, 0.1, 1.0 };
  7. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  8. GLfloat mat_shininess[] = { 100.0 };
  9. GLfloat knots[8] = { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 };
  10. GLfloat pts1[4][4][3], pts2[4][4][3];
  11. GLfloat pts3[4][4][3], pts4[4][4][3];
  12. GLUnurbsObj *nurb;
  13. int u, v;
  14.  
  15. static void 
  16. display(void)
  17. {
  18.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  19.   glCallList(1);
  20.   glFlush();
  21. }
  22.  
  23. int 
  24. main(int argc, char **argv)
  25. {
  26.   glutInit(&argc, argv);
  27.   glutCreateWindow("molehill");
  28.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  29.   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  30.   glEnable(GL_LIGHTING);
  31.   glEnable(GL_LIGHT0);
  32.   glEnable(GL_DEPTH_TEST);
  33.   glEnable(GL_AUTO_NORMAL);
  34.   glEnable(GL_NORMALIZE);
  35.   nurb = gluNewNurbsRenderer();
  36.   gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 25.0);
  37.   gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL);
  38.  
  39.   /* Build control points for NURBS mole hills. */
  40.   for(u=0; u<4; u++) {
  41.     for(v=0; v<4; v++) {
  42.       /* Red. */
  43.       pts1[u][v][0] = 2.0*((GLfloat)u);
  44.       pts1[u][v][1] = 2.0*((GLfloat)v);
  45.       if((u==1 || u == 2) && (v == 1 || v == 2))
  46.     /* Stretch up middle. */
  47.     pts1[u][v][2] = 6.0;
  48.       else
  49.     pts1[u][v][2] = 0.0;
  50.  
  51.       /* Green. */
  52.       pts2[u][v][0] = 2.0*((GLfloat)u - 3.0);
  53.       pts2[u][v][1] = 2.0*((GLfloat)v - 3.0);
  54.       if((u==1 || u == 2) && (v == 1 || v == 2))
  55.     if(u == 1 && v == 1) 
  56.       /* Pull hard on single middle square. */
  57.       pts2[u][v][2] = 15.0;
  58.     else
  59.       /* Push down on other middle squares. */
  60.       pts2[u][v][2] = -2.0;
  61.       else
  62.     pts2[u][v][2] = 0.0;
  63.  
  64.       /* Blue. */
  65.       pts3[u][v][0] = 2.0*((GLfloat)u - 3.0);
  66.       pts3[u][v][1] = 2.0*((GLfloat)v);
  67.       if((u==1 || u == 2) && (v == 1 || v == 2))
  68.     if(u == 1 && v == 2)
  69.       /* Pull up on single middple square. */
  70.       pts3[u][v][2] = 11.0;
  71.     else
  72.       /* Pull up slightly on other middle squares. */
  73.       pts3[u][v][2] = 2.0;
  74.       else
  75.     pts3[u][v][2] = 0.0;
  76.  
  77.       /* Yellow. */
  78.       pts4[u][v][0] = 2.0*((GLfloat)u);
  79.       pts4[u][v][1] = 2.0*((GLfloat)v - 3.0);
  80.       if((u==1 || u == 2 || u == 3) && (v == 1 || v == 2))
  81.     if(v == 1) 
  82.       /* Push down front middle and right squares. */
  83.       pts4[u][v][2] = -2.0;
  84.     else
  85.       /* Pull up back middle and right squares. */
  86.       pts4[u][v][2] = 5.0;
  87.       else
  88.     pts4[u][v][2] = 0.0;
  89.     }
  90.   }
  91.   /* Stretch up red's far right corner. */
  92.   pts1[3][3][2] = 6;
  93.   /* Pull down green's near left corner a little. */
  94.   pts2[0][0][2] = -2;
  95.   /* Turn up meeting of four corners. */
  96.   pts1[0][0][2] = 1;
  97.   pts2[3][3][2] = 1;
  98.   pts3[3][0][2] = 1;
  99.   pts4[0][3][2] = 1;
  100.  
  101.   glMatrixMode(GL_PROJECTION);
  102.   gluPerspective(55.0, 1.0, 2.0, 24.0);
  103.   glMatrixMode(GL_MODELVIEW);
  104.   glTranslatef(0.0, 0.0, -15.0);
  105.   glRotatef(330.0, 1.0, 0.0, 0.0);
  106.  
  107.   glNewList(1, GL_COMPILE);
  108.     /* Render red hill. */
  109.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse);
  110.     gluBeginSurface(nurb);
  111.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  112.         4 * 3, 3, &pts1[0][0][0],
  113.         4, 4, GL_MAP2_VERTEX_3);
  114.     gluEndSurface(nurb);
  115.  
  116.     /* Render green hill. */
  117.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
  118.     gluBeginSurface(nurb);
  119.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  120.         4 * 3, 3, &pts2[0][0][0],
  121.         4, 4, GL_MAP2_VERTEX_3);
  122.     gluEndSurface(nurb);
  123.  
  124.     /* Render blue hill. */
  125.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_blue_diffuse);
  126.     gluBeginSurface(nurb);
  127.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  128.         4 * 3, 3, &pts3[0][0][0],
  129.         4, 4, GL_MAP2_VERTEX_3);
  130.     gluEndSurface(nurb);
  131.  
  132.     /* Render yellow hill. */
  133.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_yellow_diffuse);
  134.     gluBeginSurface(nurb);
  135.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  136.         4 * 3, 3, &pts4[0][0][0],
  137.         4, 4, GL_MAP2_VERTEX_3);
  138.     gluEndSurface(nurb);
  139.   glEndList();
  140.  
  141.   glutDisplayFunc(display);
  142.   glutMainLoop();
  143.   return 0;             /* ANSI C requires main to return int. */
  144. }
  145.